package org.mebusw.struts.davincicode;

import junit.framework.TestCase;
import junit.framework.Assert;

public class TestCardHand extends TestCase {

	private CardHand op;
	private DavinciCard newCard;

	public TestCardHand(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();
		op = new CardHand();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testInsertSort_black_numbercard_1() {
		op.clear();
		op.insert(new DavinciCard(DavinciCard.BLACK, 3, DavinciCard.NONJOKER,
				-1), 0);
		op.insert(new DavinciCard(DavinciCard.WHITE, 5, DavinciCard.NONJOKER,
				-1), 1);

		newCard = new DavinciCard(DavinciCard.BLACK, 5, DavinciCard.NONJOKER, 0);
		Assert.assertTrue(op.validatePos(newCard, 1));
		op.insert(newCard, 1);
		Assert.assertEquals(3, op.size());
		// Assert.assertEquals("target", op.get(1).getCardId());
	}

	public void testInsertSort_black_numbercard_2() {
		op.clear();
		op.insert(new DavinciCard(DavinciCard.BLACK, 3, DavinciCard.NONJOKER,
				-1), 0);
		op.insert(new DavinciCard(DavinciCard.WHITE, 6, DavinciCard.NONJOKER,
				-1), 1);

		newCard = new DavinciCard(DavinciCard.BLACK, 5, DavinciCard.NONJOKER, 0);
		Assert.assertTrue(op.validatePos(newCard, 1));
		op.insert(newCard, 1);
		Assert.assertEquals(3, op.size());
		// Assert.assertEquals("target", op.get(1).getCardId());
	}

	public void testValidatePos_wo_joker_1() {
		op.clear();
		op.add(new DavinciCard(DavinciCard.BLACK, 3, DavinciCard.NONJOKER, 3));
		op.add(new DavinciCard(DavinciCard.BLACK, 4, DavinciCard.NONJOKER, 4));

		boolean result = op.validatePos(new DavinciCard(DavinciCard.WHITE, 3,
				DavinciCard.NONJOKER, 13), 1);
		Assert.assertEquals(true, result);
	}

	public void testValidatePos_wo_joker_2() {
		op.clear();
		op.add(new DavinciCard(false, 3, false, 13));
		op.add(new DavinciCard(true, 4, false, 4));

		boolean result = op.validatePos(new DavinciCard(false, 3, false, 3), 1);
		Assert.assertEquals(false, result);
	}

	public void testValidatePos_w_joker_1() {
		op.clear();
		op.add(new DavinciCard(true, 3, false, 3));
		op.add(new DavinciCard(true, -1, true, 9));
		op.add(new DavinciCard(false, -1, true, 19));
		op.add(new DavinciCard(true, 4, false, 4));

		boolean result = op
				.validatePos(new DavinciCard(false, 3, false, 13), 2);
		Assert.assertEquals(true, result);
	}

	public void testValidatePos_zero() {
		op.clear();

		boolean result = op.validatePos(new DavinciCard(DavinciCard.WHITE, 3,
				false, 13), 0);
		Assert.assertTrue(result);
	}

	public void testValidatePos_one_after() {
		op.clear();
		op.add(new DavinciCard(DavinciCard.BLACK, 3, false, 3));

		newCard = new DavinciCard(DavinciCard.WHITE, 3, false, 13);
		Assert.assertFalse(op.validatePos(newCard, 0));
		Assert.assertTrue(op.validatePos(newCard, 1));
	}

	public void testValidatePos_one_before() {
		op.clear();
		op.add(new DavinciCard(DavinciCard.WHITE, 3, false, 3));

		newCard = new DavinciCard(DavinciCard.BLACK, 3, false, 13);
		Assert.assertTrue(op.validatePos(newCard, 0));
		Assert.assertFalse(op.validatePos(newCard, 1));
		Assert.assertFalse(op.validatePos(newCard, 2));
		Assert.assertFalse(op.validatePos(newCard, -1));
	}

	public void testValidatePos_outOfBound() {
		op.clear();

		newCard = new DavinciCard(DavinciCard.BLACK, 3, false, 13);
		Assert.assertFalse(op.validatePos(newCard, 2));
		Assert.assertFalse(op.validatePos(newCard, -1));
	}

	public void testValidatePos_w_joker_2() {
		op.clear();
		op.add(new DavinciCard(true, 3, false, 3));
		op.add(new DavinciCard(true, -1, true, 9));
		op.add(new DavinciCard(false, -1, true, 19));
		op.add(new DavinciCard(true, 4, false, 4));

		boolean result = op
				.validatePos(new DavinciCard(false, 3, false, 13), 3);
		Assert.assertEquals(true, result);
	}

	public void testValidatePos_w_joker_3() {
		op.clear();
		op.add(new DavinciCard(true, -1, true, 9));
		op.add(new DavinciCard(false, -1, true, 19));

		boolean result = op
				.validatePos(new DavinciCard(false, 3, false, 13), 1);
		Assert.assertEquals(true, result);
	}

	public void testValidatePos_w_joker_4() {
		op.clear();
		op.add(new DavinciCard(true, -1, true, 9));
		op.add(new DavinciCard(false, -1, true, 19));

		boolean result = op
				.validatePos(new DavinciCard(false, 3, false, 13), 0);
		Assert.assertEquals(true, result);
	}

	public void testInsertSort_black_numbercard_1a() {
		op.clear();
		op.insertSort(new DavinciCard(DavinciCard.BLACK, 3,
				DavinciCard.NONJOKER, -1));
		op.insertSort(new DavinciCard(DavinciCard.WHITE, 5,
				DavinciCard.NONJOKER, -1));

		newCard = new DavinciCard(DavinciCard.BLACK, 5, DavinciCard.NONJOKER, 0);
		op.insertSort(newCard);
		Assert.assertEquals(3, op.size());
		Assert.assertEquals(3, op.get(0).getValue());
		Assert.assertEquals(5, op.get(1).getValue());
		Assert.assertEquals(DavinciCard.BLACK, op.get(1).isBlack());
		Assert.assertEquals(5, op.get(2).getValue());
		Assert.assertEquals(DavinciCard.WHITE, op.get(2).isBlack());
	}

	public void testInsertSort_black_numbercard_2a() {
		op.clear();
		op.insertSort(new DavinciCard(DavinciCard.BLACK, 3,
				DavinciCard.NONJOKER, -1));
		op.insertSort(new DavinciCard(DavinciCard.WHITE, 5,
				DavinciCard.NONJOKER, -1));

		newCard = new DavinciCard(DavinciCard.BLACK, 11, DavinciCard.NONJOKER,
				0);
		op.insertSort(newCard);
		Assert.assertEquals(3, op.size());
		Assert.assertEquals(3, op.get(0).getValue());
		Assert.assertEquals(5, op.get(1).getValue());
		Assert.assertEquals(11, op.get(2).getValue());
	}

	public void testReinsertSortLast() {
		op.clear();
		newCard = new DavinciCard(DavinciCard.BLACK, 11, DavinciCard.NONJOKER,
				0);
		op.insertLast(newCard);
		Assert.assertEquals(11, op.getLast().getValue());
		newCard = new DavinciCard(DavinciCard.BLACK, 0, DavinciCard.NONJOKER, 0);
		op.insertLast(newCard);
		Assert.assertEquals(0, op.getLast().getValue());
		op.reinsertSortLast();
		Assert.assertEquals(0, op.get(0).getValue());
		Assert.assertEquals(11, op.getLast().getValue());

	}

	public void testGetStandingCardCount() {
		op.clear();
		op.insertSort(new DavinciCard(DavinciCard.BLACK, 3,
				DavinciCard.NONJOKER, -1));
		op.insertSort(new DavinciCard(DavinciCard.WHITE, 5,
				DavinciCard.NONJOKER, -1));
		op.insertSort(new DavinciCard(DavinciCard.WHITE, 2,
				DavinciCard.NONJOKER, -1));
		
		op.get(0).setUncover(DavinciCard.STANDING);
		op.get(1).setUncover(DavinciCard.TURNOVER);
		op.get(2).setUncover(DavinciCard.STANDING);
		Assert.assertEquals(DavinciCard.TURNOVER, op.get(1).isUncover());
		Assert.assertEquals(3, op.size());
		Assert.assertEquals(2, op.getStandingCardCount());
	}
}
